package com.spider.silence.job;

import com.spider.silence.downloader.ChinaNewsForSiChuanDownloader;
import com.spider.silence.pool.ChinaNewsForSiChuanDownloaderPool;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

/**
 * Created by Linbo Hu on 2017/7/4.
 * 中国新闻网四川频道爬虫定时任务
 */
public class ChinaNewsForSiChuanJob implements StatefulJob {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ChinaNewsForSiChuanDownloaderPool downloaderPool = ChinaNewsForSiChuanDownloaderPool.getInstance();
        //获取一个下载器
        ChinaNewsForSiChuanDownloader downloader = null;
        try {
            downloader = downloaderPool.fetchDownLoader(1000);
            while (true) {
                if(downloader != null) {
                    break;
                } else {
                    downloader = downloaderPool.fetchDownLoader(1000);
                }
            }
            downloader.generateTask();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if(downloader != null) {
                downloaderPool.releaseDownLoader(downloader);
            }
        }
    }
}
